var app = getApp();
var baseUrl = app.globalData.host;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tlrCode: '',
    personalList: {},
    custName:'',
    pageSize:10,
    pageNo:1,
    hasMoreData: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
        tlrCode:options.tlrCode
    });
    var that = this
    that.getSelectCustomer('正在加载数据...');
   },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('下拉');
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.data.pageNum = 1
    this.getSelectCustomer('正在刷新数据')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.info('上拉');
    console.log(this.data.hasMoreData);
    if (this.data.hasMoreData) {
      this.getSelectCustomer('加载更多数据')
    } else {
      wx.showToast({
        title: '没有更多数据',
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  setReport: function(e){
    wx.navigateTo({
      url: '../../report/index?custId='+e.currentTarget.dataset.value.custId+"&custName="+e.currentTarget.dataset.value.custName
    })
  },
  //筛选
  wxSearchBlur: function(e){
    var that = this
    that.setData({
      custName:e.detail.value,
      pageSize:10,
      pageNo:1,
    });
    that.getSelectCustomer('正在刷新数据');
  },

  getSelectCustomer: function(message){
    wx.showLoading({
      title: message,
    })
    var that = this;
    let obj = {
      tlrCode: that.data.tlrCode,
      pageSize: that.data.pageSize,
      pageNo:that.data.pageNo,
      custName:that.data.custName,
    }
    wx.request({
      url: baseUrl+"/user/selectPersonal",
      method: 'POST',
      data: obj,
      header: {
          'content-type': 'application/json'
      },
      dataType: 'json',
      success: function(res) { 
        console.log(res);
        var personalListTem = that.data.personalList;
        if(res.statusCode==200){
          if (that.data.pageNo == 1) {
            personalListTem = []
          }
          var personalList = res.data.data;
          console.info(personalList.length);
          if (personalList.length < 10){
            that.setData({
              personalList: personalListTem.concat(personalList),
              hasMoreData: false
            })
          }else{
            that.setData({
              personalList: personalListTem.concat(personalList),
              hasMoreData: true,
              pageNo: that.data.pageNo + 1
            })
          }
        }
      },
      fail: function(res) {
          wx.showToast({
          title: '服务器网络错误,请稍后重试',
          icon: 'loading',
          duration: 1500
          })
      },
      complete: function(res) {
        wx.hideLoading();
      },
    })
  }
})